<?php
class CommentsController extends CController {
	public $defaultAction = 'manage';
	
	public function filters() {
		return array(
				'accessControl'
		);
	}
	
	
	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}
	
	public function actionManage() {
		$criteria = new CDbCriteria;
		$with = array('author');
		$count = Comments::model()->with($with)->count($criteria);
		// Pagination
		$pages = new CPagination($count);
		$pages->pageSize = Yii::app()->params['manageListPerPage'];
		$pages->applyLimit($criteria);
		// Get rows
		$comments = Comments::model()->findAll($criteria);
		
		$this->render('commentsManage', array(
			'comments' => $comments,
			'pages' => $pages,
			'count' => $count
		));
	}
	
	public function actionPsytestcommentmanage() {
		$criteria = new CDbCriteria;
		$with = array('author', 'topic');
		$count = TestComments::model()->with($with)->count($criteria);
		// Pagination
		$pages = new CPagination($count);
		$pages->pageSize = Yii::app()->params['manageListPerPage'];
		$pages->applyLimit($criteria);
		// Get rows
		$comments = TestComments::model()->findAll($criteria);

		$this->render('psytest-comments', array(
			'comments' => $comments,
			'pages' => $pages,
			'count' => $count
		));
	}
	
	public function actionDeletepsytestcomment() {
		$commentId = isset($_GET['id']) ? (int)$_GET['id'] : null;
		
		if($commentId !== null) {
			$comment = TestComments::model()->findByPk($commentId);
			if($comment !== null && $comment->delete()) {
				Yii::app()->user->setFlash('successMessage', '评论删除成功');
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
	}

	public function actionDelete() {
		$commentId = isset($_GET['id']) ? (int)$_GET['id'] : -1;
		
		if($commentId > 0) {
			$comment = Comments::model()->findByPk($commentId);
			if($comment !== null && $comment->delete()) {
				$msgs = array('save' => '删除成功。');
				Yii::app()->user->setFlash('msgs', $msgs);
				$this->redirect(array('/admin/comments'));
			}
		}
	}
}